package org.feifei.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.feifei.basic.util.CommonResult;
import org.feifei.basic.util.PageList;
import org.feifei.org.domain.Department;
import org.feifei.org.query.DepartmentQuery;
import org.feifei.org.service.DepartmentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Department接口
 */
@RestController
@Api(tags = "DepartmentController", description = "部门接口")
@RequestMapping("/department")
//@CrossOrigin("http://127.0.0.1:8080")
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    /**
     * 不带条件查询所有
     * @return
     */
    @GetMapping("/all")
    @ApiOperation(value = "不带条件查询所有", notes = "直接查看该表下的所有数据")
    public CommonResult list() {
        try {
            List<Department> list = departmentService.list();
            return CommonResult.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        }
    }

    /**
     * 通过id获取单个实体类的数据
     * @param id Department的id
     * @return
     */
    @ApiOperation(value = "通过id获取单个实体类的数据", notes = "根据id,来获取Department中,是输入值id的部门 ")
    @GetMapping("/{id}")
    public CommonResult getById(@PathVariable("id")Long id) {
        try {
            Department department = departmentService.get(id);
            return CommonResult.ok(department);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        }
    }

    /**
     * 根据id删除
     * @param id Department的id
     * @return
     */
    @ApiOperation(value = "根据id删除部门信息", notes = "通过传入的部门id,先删除该部门的相关信息，再删除部门信息")
    @DeleteMapping("/{id}")
    public CommonResult del(@PathVariable("id")Long id) {
        try {
            departmentService.remove(id);
            return CommonResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        }
    }

    /**
     * 添加方法
     * @param department 实体类 Department
     * @return
     */
    @ApiOperation(value = "添加新的部门",notes = "填入新的部门信息，来创建一个新的部门")
    @PostMapping
    public CommonResult add(@RequestBody Department department) {
        try {
            departmentService.save(department);
            return CommonResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        }
    }

    /**
     * 修改的方法
     * @param department 实体类 Department
     * @return
     */
    @ApiOperation(value = "修改部分的信息", notes = "根据传入的部门实体类，需要携带部门id，来修改该部门的信息")
    @PutMapping
    @Deprecated
    public CommonResult update(@RequestBody Department department) { // @RequestBody 从请求体中获取参数
        try {
            departmentService.update(department);
            return CommonResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        }
    }

    /**
     * 高级查询，分页查询，条件查询
     * @param query 查询类 DepartmentQuery
     * @return
     */
    @ApiOperation(value = "根据查询条件", notes = "包含了分页查询，高级查询")
    @PostMapping("/list")
    public PageList<List<Department>> listByPage(@RequestBody DepartmentQuery query) {
        return departmentService.listByPage(query);
    }

    /**
     * 批量删除，根据传过来的id的集合，批量删除
     *
     * @param ids id集合
     * @return
     */
    @ApiOperation(value = "批量删除", notes = "根据传过来的删除数据")
    @PatchMapping
    public CommonResult bacthRemove(@RequestBody List<Long> ids) {
        try {
        departmentService.batchRemove(ids);
        return CommonResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure();
        }
    }

    /**
     * 获取所有部门的数据以及结构
     * @return
     */
    @ApiOperation(value = "获取部门树",notes = "获取所有部门的数据以及结构")
    @GetMapping("/deptTree")
    public  List<Department> getDepartTree() {
       return departmentService.getDepartTree();
    }
}
